#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define y1 y_alpha_1

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

const int N = 1e6 + 10;

int a[N],p[N],n,m,w[N],tong[N];
int rt;

namespace st {
	struct node { ll s,ls,rs,ans; node (ll t = 0) { s = t; ls = rs = ans = max(t,0ll); } } val[N << 1];
	int ch[N << 1][2],nodenum;
	node operator + (node a,node b) {
		node c; c.s = a.s + b.s; c.ls = max(a.ls,a.s + b.ls); c.rs = max(b.rs,b.s + a.rs);
		c.ans = max(a.ans,max(b.ans,a.rs + b.ls)); return c;
	}
	void upd(int pos,ll v,int &o = rt,int l = 1,int r = n) {
		if(!o) o = ++nodenum; if(l == r) return val[o] = node(v),void(); int mid = l + r >> 1;
		if(pos <= mid) upd(pos,v,ch[o][0],l,mid); else upd(pos,v,ch[o][1],mid + 1,r);
		val[o] = val[ch[o][0]] + val[ch[o][1]];
	}
	ll qmax() { return val[1].ans; }
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in; for(int i = 1;i <= n;i++) a[i] = in,p[i] = tong[a[i]],tong[a[i]] = i;
	for(int i = 1;i <= m;i++) w[i] = in; ll ans = 0;
	for(int i = 1;i <= n;i++) {
		if(p[p[i]]) st :: upd(p[p[i]],0); if(p[i]) st :: upd(p[i],-w[a[i]]); st :: upd(i,w[a[i]]);
		ans = max(ans,st :: qmax());
	}
	printf("%lld\n",ans);
	return 0;
}
